# -*- coding: utf-8 -*-

# 导入必要的工具包
# 独立调用xgboost或在sklearn框架下调用均可。
# 1. 模型训练：超参数调优
#     1. 初步确定弱学习器数目： 20分
#     2. 对树的最大深度（可选）和min_children_weight进行调优（可选）：20分
#     3. 对正则参数进行调优：20分
#     4. 重新调整弱学习器数目：10分
#     5. 行列重采样参数调整：10分
# 2. 调用模型进行测试10分
# 3. 生成测试结果文件10分

import xgboost as xgb
from xgboost import XGBClassifier

from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import StratifiedKFold

from sklearn.metrics import log_loss

import pandas as pd
import numpy as np

dpath = './data/'
train = pd.read_csv(dpath + 'RentListingInquries_FE_train_sample.csv')

train_X = train.drop("interest_level", axis=1)
train_y = train["interest_level"]

# # 各类样本不均衡，交叉验证是采用StratifiedKFold，在每折采样时各类样本按比例采样
# # prepare cross validation
from sklearn.model_selection import StratifiedKFold
kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=3)

#第一轮参数调整得到的n_estimators 是 277
estimators = 50
max_depth4 = 4
min_child_weight4 = 1

xgb4 = XGBClassifier(
        learning_rate =0.1,
        n_estimators=estimators,
        max_depth= max_depth4,
        min_child_weight= min_child_weight4,
        gamma=0,
        subsample=0.3,
        colsample_bytree=0.8,
        colsample_bylevel = 0.7,
        objective= 'multi:softmax',
        nthread=-1,
        seed=3)

reg_alpha = [1e-3, 1e-2, 0.05, 0.1]    #default = 0
reg_lambda = [1e-3, 1e-2, 0.05, 0.1]   #default = 1
param_test_4 =  dict(reg_alpha=reg_alpha, reg_lambda=reg_lambda)
gsearch4 = GridSearchCV(xgb4, param_grid = param_test_4, scoring='neg_log_loss',n_jobs=-1, cv=kfold)
gsearch4.fit(train_X , train_y)

print gsearch4.grid_scores_
print gsearch4.best_params_
print gsearch4.best_score_

# FIXME
# [mean: -0.64290, std: 0.01362, params: {'reg_alpha': 0.001, 'reg_lambda': 0.001}, mean: -0.64310, std: 0.01379, params: {'reg_alpha': 0.001, 'reg_lambda': 0.01}, mean: -0.64410, std: 0.01475, params: {'reg_alpha': 0.001, 'reg_lambda': 0.05}, mean: -0.64455, std: 0.01456, params: {'reg_alpha': 0.001, 'reg_lambda': 0.1}, mean: -0.64375, std: 0.01407, params: {'reg_alpha': 0.01, 'reg_lambda': 0.001}, mean: -0.64432, std: 0.01429, params: {'reg_alpha': 0.01, 'reg_lambda': 0.01}, mean: -0.64299, std: 0.01320, params: {'reg_alpha': 0.01, 'reg_lambda': 0.05}, mean: -0.64361, std: 0.01337, params: {'reg_alpha': 0.01, 'reg_lambda': 0.1}, mean: -0.64357, std: 0.01425, params: {'reg_alpha': 0.05, 'reg_lambda': 0.001}, mean: -0.64321, std: 0.01424, params: {'reg_alpha': 0.05, 'reg_lambda': 0.01}, mean: -0.64310, std: 0.01339, params: {'reg_alpha': 0.05, 'reg_lambda': 0.05}, mean: -0.64399, std: 0.01334, params: {'reg_alpha': 0.05, 'reg_lambda': 0.1}, mean: -0.64387, std: 0.01393, params: {'reg_alpha': 0.1, 'reg_lambda': 0.001}, mean: -0.64347, std: 0.01396, params: {'reg_alpha': 0.1, 'reg_lambda': 0.01}, mean: -0.64371, std: 0.01420, params: {'reg_alpha': 0.1, 'reg_lambda': 0.05}, mean: -0.64259, std: 0.01349, params: {'reg_alpha': 0.1, 'reg_lambda': 0.1}]
# {'reg_alpha': 0.1, 'reg_lambda': 0.1}
# -0.642588433949

# estimators = 50
# max_depth4 = 4
# min_child_weight4 = 1
# [mean: -0.65844, std: 0.00943, params: {'reg_alpha': 0.001, 'reg_lambda': 0.001}, mean: -0.66106, std: 0.00913, params: {'reg_alpha': 0.001, 'reg_lambda': 0.01}, mean: -0.65861, std: 0.00820, params: {'reg_alpha': 0.001, 'reg_lambda': 0.05}, mean: -0.65907, std: 0.00812, params: {'reg_alpha': 0.001, 'reg_lambda': 0.1}, mean: -0.65821, std: 0.00802, params: {'reg_alpha': 0.01, 'reg_lambda': 0.001}, mean: -0.65987, std: 0.00931, params: {'reg_alpha': 0.01, 'reg_lambda': 0.01}, mean: -0.65756, std: 0.01048, params: {'reg_alpha': 0.01, 'reg_lambda': 0.05}, mean: -0.65790, std: 0.00920, params: {'reg_alpha': 0.01, 'reg_lambda': 0.1}, mean: -0.65517, std: 0.00923, params: {'reg_alpha': 0.05, 'reg_lambda': 0.001}, mean: -0.65569, std: 0.00966, params: {'reg_alpha': 0.05, 'reg_lambda': 0.01}, mean: -0.65737, std: 0.01226, params: {'reg_alpha': 0.05, 'reg_lambda': 0.05}, mean: -0.65951, std: 0.00841, params: {'reg_alpha': 0.05, 'reg_lambda': 0.1}, mean: -0.65833, std: 0.00968, params: {'reg_alpha': 0.1, 'reg_lambda': 0.001}, mean: -0.66055, std: 0.01149, params: {'reg_alpha': 0.1, 'reg_lambda': 0.01}, mean: -0.66147, std: 0.01080, params: {'reg_alpha': 0.1, 'reg_lambda': 0.05}, mean: -0.65829, std: 0.00693, params: {'reg_alpha': 0.1, 'reg_lambda': 0.1}]
# {'reg_alpha': 0.05, 'reg_lambda': 0.001}
# -0.6551683277



